package com.eeepay.eeepay_v2.api;

import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.eeepay.api.grpc.nano.ManagementCenterProto;
import cn.eeepay.api.grpc.nano.SuperPushServiceGrpc;
import io.grpc.ManagedChannel;

/**
 * 描述：获取邀请有奖商户信息记录 API
 * 一级代理商需要有邀请有奖的切换，二级及以下代理商还是按照原来的，不用加切换
 * 作者：zhuangzeqin
 * 时间: 2017/8/21-13:35
 * 邮箱：zzq@eeepay.cn
 */
public class GetInvPriMerInfoRecordAPI {
    private ResultCallBack resultCallBack;//设置监听接口
    private int mTotal;//总的记录数

    public final static int PAGESIZE = 10;//每页显示10条
    /**
     * 设置监听接口
     *
     * @param resultCallBack
     */
    public void setResultCallBack(ResultCallBack resultCallBack) {
        this.resultCallBack = resultCallBack;
    }

    /**
     * 获取邀请有奖商户信息记录 API
     * @param currPage 当前页面
     */
    public void getInvPriMerInfoRecordInfo(final int currPage)
    {
        if (resultCallBack == null) {
            throw new RuntimeException("ResultCallBack is null,you can must implement.");
        }
        if (currPage == 0) {
            throw new RuntimeException("currPage is not zero.");
        }
        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT, 0, new GrpcUtils.AsyncTaskListener() {
            @Override
            public Object doInBack(ManagedChannel channel, int taskId) {
                SuperPushServiceGrpc.SuperPushServiceBlockingStub stub =
                        SuperPushServiceGrpc.newBlockingStub(channel).withDeadlineAfter(30, TimeUnit.SECONDS);
                ManagementCenterProto.InvPriMerInfoRecordReq req = new ManagementCenterProto.InvPriMerInfoRecordReq();
                req.page = currPage;//当前页
                req.pageSize = PAGESIZE;//页码数
                req.userId = UserInfo.getUserInfo2SP().getAgentNo();//调用查询的用户ID，代理商传代理商编号
                return stub.getInvPriMerInfoRecord(req);
            }

            @Override
            public void updateUI(Object result, int taskId) {
                if (result == null) {
                    resultCallBack.onFail("查询失败,请稍后重试!");
                    return;
                }
                ManagementCenterProto.InvPriMerInfoRecordRes invPriMerInfoRecordRes = (ManagementCenterProto.InvPriMerInfoRecordRes)result;
                if (!invPriMerInfoRecordRes.resStatus.status) {
                    resultCallBack.onFail(invPriMerInfoRecordRes.resStatus.msg);//返回错误信息
                    return;
                }
                mTotal = invPriMerInfoRecordRes.total;
//                if (mTotal==0)
//                {
//                    resultCallBack.onFail("暂时没有相关记录,请稍后重试!");
//                    return;
//                }
                ManagementCenterProto.InvitePrizesMerchantInfoRecord[] invitePrizesRecord = invPriMerInfoRecordRes.invitePrizesRecord;
                resultCallBack.onSucess(currPage,mTotal, Arrays.asList(invitePrizesRecord));
            }
        });
    }

    /**
     * 提供外围接口；通过接口回传
     */
    public interface ResultCallBack {
        void onFail(String msg);//失败返回；提示语

        void onSucess(int currPage, int total, List<ManagementCenterProto.InvitePrizesMerchantInfoRecord> prizesMerchantInfoRecords);//成功返回列表数据数据
    }
}
